#coding=utf-8
__author__ = 'Meteor'
import json,jsonpath
import re
from common.log import logger

# json等值断言（1、接收响应报文，2、jsonpath路径，3、预期结果）
def assert_json_eq(response,jpath,exprt):
    '''
    
    :param response: response
    :param jpath: 
    :param exprt: 
    :return: 
    '''
    # response本身是一个字符串的文本内容（json串），需要转换成字典
    response = json.loads(response.text)
    # logger.info('当前请求的响应为: %s' %response)

    # logger.info('json Path:%s , exprt : %s' %(jpath,exprt))
    assert  jsonpath.jsonpath(response,jpath)[0] == exprt


def assert_json_in(response,jpath,exprt):
    '''
    
    :param response:  response
    :param jpath: 
    :param exprt: 
    :return: 
    '''
    response = json.loads(response.text)
    assert  exprt in jsonpath.jsonpath(response,jpath)


def assert_re_eq(response,pattern,exprt):
    '''
    :param
    response: response.text
    '''
    assert re.findall(pattern,response.text)[0] == exprt

def assert_re_in(respose,pattern,exprt):
    '''
    
    :param respose: response.text
    :param pattern: 
    :param exprt: 
    :return: 
    '''
    assert exprt in re.findall(pattern,respose.text)


# response是响应对象
def assert_status_code(response,code):
    '''   
    :param response: response
    :param code: 
    :return: 
    '''
    assert response.status_code == code
